package cn.edu.lcvc.ebuy_ruanjian2.web.controller.backstage.adminmanage;

import cn.edu.lcvc.ebuy_ruanjian2.model.Admin;
import cn.edu.lcvc.ebuy_ruanjian2.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/backstage/adminmanage")
public class AdminManageController {

    @Autowired
    public AdminService adminService;

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    //跳转到管理员列表
    public String readAdmins(HttpServletRequest request) {
        List<Admin> admins = adminService.getAdmins();
        request.setAttribute("list", admins);
        //跳转到管理员页面
        return "/jsp/backstage/adminmanage/adminmanage.jsp";
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String deleteAdmin(Integer id, HttpSession session) {
            //删除指定账户
            adminService.deleteAdmin(id);
            //返回管理员列表页面
            return "/backstage/adminmanage/all";
    }

    //跳转到账户添加页面
    @RequestMapping(value = "/toAddAdmin", method = RequestMethod.GET)
    public String toAddAdmin() {
        return "/jsp/backstage/adminmanage/adminadd.jsp";
    }

    //执行账户添加请求,将页面传递过来的账户信息存储到数据库
    @RequestMapping(value = "/doAddAdmin", method = RequestMethod.POST)
    public String doAddAdmin(HttpServletRequest request, Admin admin) {
        if (admin.getUsername().equals("")) {
            request.setAttribute("message", "账户名不能为空");
            return "/jsp/backstage/adminmanage/adminadd.jsp";
        } else if (admin.getName().equals("")) {
            request.setAttribute("message", "姓名不能为空");
            return "/jsp/backstage/adminmanage/adminadd.jsp";
        } else if (admin.getSex() == null) {
            request.setAttribute("message", "必须选择性别");
            return "/jsp/backstage/adminmanage/adminadd.jsp";
        } else {
            //接收页面传递的参数并保存到数据库
            request.setAttribute("message", "账户添加成功!");
            adminService.saveAdmin(admin);
            //跳转到账户添加页面
            return "/jsp/backstage/adminmanage/adminadd.jsp";
        }
    }

    //跳转到账户编辑页面
    @RequestMapping(value = "/toUpdateAdmin", method = RequestMethod.GET)
    public String toUpdateAdmin(HttpServletRequest request, Integer id) {
        if (id != null) {
            //从数据库中找出要编辑的账户信息
            Admin admin = adminService.getAdmin(id);
            request.setAttribute("admin", admin);
        } else {
            request.setAttribute("message", "参数非法");
        }
        return "/jsp/backstage/adminmanage/adminedit.jsp";
    }

    //处理账户编辑请求
    @RequestMapping(value = "/doUpdateAdmin", method = RequestMethod.POST)
    public String doUpdateAdmin(HttpServletRequest request,Admin admin){
        //处理编辑请求
        if (admin.getUsername().equals("")) {
            request.setAttribute("message", "账户名不能为空");
        } else if (admin.getName().equals("")) {
            request.setAttribute("message", "姓名不能为空");
        } else if (admin.getSex() == null) {
            request.setAttribute("message", "必须选择性别");
        }else {//如果验证通过
            request.setAttribute("message", "账户编辑成功!");
            adminService.updayeAdmin(admin);
        }
       //处理完成之后
        return "/jsp/backstage/adminmanage/adminedit.jsp";
    }

}
